package com.cxl.service.impl;

import com.cxl.entity.UserCoupon;
import com.cxl.entity.Users;
import com.cxl.repository.CouponRepository;
import com.cxl.repository.UsersCouponRepository;
import com.cxl.repository.UsersRepository;
import com.cxl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.apache.commons.lang.StringUtils;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: 20971
 * @time: 2022/3/3 21:44
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UsersRepository usersRepository;

    @Override
    public Page<Users> queryList(Users users, Integer pageNum, Integer pageSize) {

        //将查询条件封装存在Specification这个对象中
        Specification<Users> specification = new Specification<Users>() {
            //重新Specification的toPredicate方法
            @Override
            public Predicate toPredicate(Root<Users> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = null ;
                if (StringUtils.isNotBlank(users.getUsername())) {
                    //criteriaBuilder.like模糊查询，第一个参数是根据root.get("字段名称")获取查询字段,第二个参数是like('%xxx%')中，xxx的值
                    predicate = criteriaBuilder.like(root.get("username"), "%" + users.getUsername() + "%");
                }
                return predicate;
            }
        };
        //分页条件存在这个对象中
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize);
        //进行查询操作，第一个参数是查询条件对象，第二个参数是分页对象
        Page<Users> page = usersRepository.findAll(specification, pageRequest);
        //返回的数据都封装在了Page<Admin>对象中
        return page;
    }

    @Override
    public Users save(Users users) {
        return usersRepository.save(users);
    }

    @Autowired
    private UsersCouponRepository usersCouponRepository;

    @Autowired
    private CouponRepository couponRepository;

    @Override
    public void del(Integer id) {
        //删除中间表
        Specification<UserCoupon> specification = new Specification<UserCoupon>() {
            //重新Specification的toPredicate方法
            @Override
            public Predicate toPredicate(Root<UserCoupon> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                return criteriaBuilder.equal(root.get("users"), id);
            }
        };
        List<UserCoupon> all = usersCouponRepository.findAll(specification);
        for (UserCoupon userCoupon : all) {
            usersCouponRepository.delete(userCoupon);
        }
        //删主表
        usersRepository.delete(usersRepository.getById(id));
    }
}
